package com.seocoo.worker.user.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.seocoo.worker.R;
import com.seocoo.worker.base.CommWebActivity;
import com.seocoo.worker.base.Const;
import com.seocoo.worker.base.KeyboardUtil;
import com.seocoo.worker.base.widget.EditTextCloseView;
import com.seocoo.worker.home.HomeActivity;
import com.seocoo.worker.user.login.model.LoginModel;
import com.seocoo.worker.user.login.model.ReqLogin;
import com.seocoo.worker.user.login.model.ReqSMS;
import com.sfh.lib.AppCacheManager;
import com.sfh.lib.mvvm.LiveDataMatch;
import com.sfh.lib.ui.MVVMActivity;
import com.sfh.lib.utils.ThreadTaskUtils;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class LoginActivity extends MVVMActivity implements View.OnClickListener {


    public static void start(Context context) {
        context.startActivity(new Intent(context, LoginActivity.class));
    }

    EditTextCloseView et_name;
    EditTextCloseView et_code;
    TextView tv_register;
    TextView tv_code;
    TextView tv_login;
    TextView tv_agree;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.user_login_activity);
        et_name = findViewById(R.id.et_name);
        et_code = findViewById(R.id.et_code);

        tv_register = findViewById(R.id.tv_register);
        tv_code = findViewById(R.id.tv_code);
        tv_login = findViewById(R.id.tv_login);
        tv_agree = findViewById(R.id.tv_agree);


        StatusBarUtil.setTranslucentForImageView(this, 0, findViewById(R.id.tv_title));

        tv_register.setOnClickListener(this);
        tv_code.setOnClickListener(this);
        tv_login.setOnClickListener(this);
        tv_agree.setOnClickListener(this);

        String phone = AppCacheManager.getCacheListener().getString("phone", "");
        if (!TextUtils.isEmpty(phone)) {
            et_name.setText(phone);
        }
    }


    @Override
    public void onClick(View v) {
        KeyboardUtil.hideKeyboard(v);
        if (v == tv_register) {
            // 注册
            RegisterActivity.start(this);
        } else if (v == tv_code) {
            //发验证码
            String phone = et_name.getText().toString();
            if (TextUtils.isEmpty(phone)) {
                showToast("请输入手机号");
                return;
            }

            getViewModel(LoginModel.class).sendSMS(new ReqSMS(phone, "2"));

        } else if (v == tv_agree) {
            // 协议
            CommWebActivity.start(getActivity(),"协议", Const.API +"/dwm/protocol.html");
        } else if (v == tv_login) {
            //登录

            String phone = et_name.getText().toString().trim();
            if (TextUtils.isEmpty(phone)) {
                showToast("请输入手机号");
                return;
            }
            String code = et_code.getText().toString().trim();
            if (TextUtils.isEmpty(code)) {
                showToast("请输入验证码");
                return;
            }
            getViewModel(LoginModel.class).login(new ReqLogin(phone, code));
        }
    }

    @LiveDataMatch
    public void onLoginSuccess() {
        String phone = et_name.getText().toString().trim();
        AppCacheManager.getPersistListener().putString("phone", phone, true);
        HomeActivity.startContentUI(this);
        finish();
    }

    @LiveDataMatch
    public void onGotoRegisterUI() {
        RegisterActivity.start(this);
    }

    private int sizeCount;
    private Future sizeFuture;

    @LiveDataMatch
    public void onSendCodeSuccess() {
        sizeCount = 60;
        if (sizeFuture != null && !sizeFuture.isCancelled()) {
            sizeFuture.cancel(true);
            sizeFuture = null;
        }
        sizeFuture = ThreadTaskUtils.scheduleWithFixedDelay(() -> {
            runOnUiThread(() -> {
                sizeCount--;
                if (sizeCount > 0) {
                    tv_code.setEnabled(false);
                    tv_code.setText(String.format("%sS", sizeCount));

                } else {
                    sizeFuture.cancel(true);
                    tv_code.setEnabled(true);
                    tv_code.setText("获取验证码");
                }
            });
        }, 1000, 1000, TimeUnit.MILLISECONDS);
        this.putFuture(sizeFuture);
        //发验证码

    }

}
